<template>
  <el-main class="login-container">
    <el-form :model="user" ref="user" :rules="rules" label-width="100px" class="login-form" @keyup.enter.native="login"">

    <el-form-item label="登录名称" prop="username">
      <el-input v-model="user.username" placeholder="请输入登录名称" clearable></el-input>
    </el-form-item>

    <el-form-item label="登录密码" prop="password">
      <el-input v-model="user.password" type="password" placeholder="请输入登录密码" clearable></el-input>
    </el-form-item>

    <el-form-item>
      <el-button class="login-btn" type="primary" @click="login">登录</el-button>
    </el-form-item>

    </el-form>
  </el-main>
</template>

<script>
export default {
  data(){
    return {
      user:{
        username:'zhangsan',
        password:'123456',
        token: null
      },
      token: null,
      base64Images: null,
      rules:{
        username:[
          { required: true, message: '请输入登录名', trigger: 'blur' }
        ],
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    login(){
      let user = this.user;
      console.log(user);

      //发送ajax请求，后台验证用户，如果验证成功，给前台返回一个token（用户id或登录名称）
      this.axios.post("user/login",user).then(res=>{
        if (res.data.code = 200) {
          //存储用户信息到localStorege
          let token = res.data.data;
          localStorage.setItem("token", token);
          this.$router.replace("/index");
        } else {
          this.$message(res.data.msg);
        }
      })
    }
  },
  created(){
    localStorage.removeItem("token");
  }
}
</script>

<style>
/* 登录容器样式 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

/* 登录表单样式 */
.login-form {
  width: 400px;
  padding: 24px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-left: 30%;
  margin-top: 10%;
}

/* 验证码容器样式 */
.code-wrap {
  display: flex;
  align-items: center;
}

/* 验证码输入框样式 */
.code-input {
  flex: 1;
  font-size: 14px;
}

/* 验证码图片容器样式 */
.code-img-wrap {
  margin-left: 10px;
}

/* 验证码图片样式 */
.code-img {
  width: 100px;
  height: 32px;
  border-radius: 4px;
  cursor: pointer;
  background-color: #fff;
  border: 1px solid #dcdfe6;
}

/* 登录按钮样式 */
.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  border-radius: 4px;
}

/* 更改 el-button 默认背景色和圆角 */
.el-button--primary {
  background-color: #409eff;
  border-color: #409eff;
  border-radius: 4px;
}

/* 更改 el-input 组件的默认高度和字体大小 */
.el-input {
  height: 48px;
  font-size: 14px;
}
</style>